package leetcode.周赛.第245场;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/6/13 10:28
 * @description：https://leetcode-cn.com/problems/redistribute-characters-to-make-all-strings-equal/
 */
public class 第2题_可移除字符的最大数目 {
    @Test
    public void Test() {
        Solution solution = new Solution();
        System.out.println(solution.maximumRemovals("abcacb", "ab", new int[]{3,1,0}));
    }

    class Solution {
        public int maximumRemovals(String s, String p, int[] removable) {
            boolean[] removed = new boolean[s.length()];
            int l = 0;
            int r = removable.length;
            while(l < r) {
                int mid = l + r + 1 >> 1;
                Arrays.fill(removed, false);
                for(int i = 0; i < mid; i++) {
                    removed[removable[i]] = true;
                }
                if (isSub(removed, s, p)) {
                    l = mid;
                } else {
                    r = mid - 1;
                }
            }
            return l;
        }
        public boolean isSub(boolean[] removed, String str, String sub) {
            int i = 0;
            int j = 0;
            while(i < str.length() && j < sub.length()) {
                if (removed[i]) {
                    i++;
                    continue;
                }
                if (str.charAt(i) == sub.charAt(j)) j++;
                i++;
            }
            return j == sub.length();
        }
    }
}
